﻿/// <reference path="../game/fourkill.util.js" />
/// <reference path="../painters/fourkill.painter.singletons.js" />
/// <reference path="../objects/fourkill.object.singletons.js" />
/// <reference path="../input/fourkill.input.singletons.js" />

var FourKillGameViewMap = Base.extend({
    initialize: function initialize(host) {
        this.host = host;
        this.paintingContext = null;
        this.painters = [];
        this.objects = [];
        this.inputs = [];
        this.paintInterval = null;
        this.frameRate = 100;
        this.viewId = "fourkill_minimal";
        this.isActive = false;
        this.volume = 0.0;
    },
    setup: function setup() {

        var $backCanvas = $('<canvas/>', { 'class': 'radHuh', 'id': 'backCanvas' })
           .attr("width", $(window).width())
           .attr("height", $(window).height())
           .offset({ top: 0, left: 0 })
           .css("position", "absolute")
           .css("zIndex", "0");

        var $foreCanvas = $('<canvas/>', { 'class': 'radHuh', 'id': 'foreCanvas' })
           .attr("width", $(window).width())
           .attr("height", $(window).height())
           .offset({ top: 0, left: 0 })
           .css("position", "absolute")
           .css("zIndex", "1");

        $(document.body).append($backCanvas);
        $(document.body).append($foreCanvas);
        
        this.paintingContext = PaintingContext.newInstance();
        this.paintingContext.backLayer = $backCanvas.get(0).getContext("2d");
        this.paintingContext.frontLayer = $foreCanvas.get(0).getContext("2d");
        this.paintingContext.viewPort = ViewPort.newInstance(6, util._90, util._45);
        this.paintingContext.camera = Placement.newInstance();
        this.paintingContext.camera.w = 3.0;
        this.paintingContext.camera.h = 6.25;
        this.paintingContext.camera.x = 30.0;
        this.paintingContext.camera.y = 10.0;
        this.paintingContext.camera.setBearing(util._90);

        this.painters.push(Painters.Sky2);
        this.painters.push(Painters.Ground);

        this.objects.push(MistObject.newInstance(1000, 0.2));

        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].isAvailable()) {
                this.inputs[k].enable();
            }
        }

        this.paintInterval = setInterval((function (self) { return function () { self.paint(); } })(this), this.frameRate);
        this.isActive = true;
    },
    teardown: function teardown() {
        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].disable();
            }
        }
        $(this.paintingContext.backLayer.canvas).remove();
        $(this.paintingContext.frontLayer.canvas).remove();
        this.paintingContext = null;
        this.painters.length = 0;
        this.inputs.length = 0;
        this.objects.length = 0;
        if (this.paintInterval != null) {
            clearInterval(this.paintInterval);
            this.paintInterval = null;
        }
        this.isActive = false;
    },
    pause: function pause() {
        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].disable();
            }
        }
        if (this.paintInterval != null) {
            clearInterval(this.paintInterval);
            this.paintInterval = null;
        }
    },
    resume: function resume() {
        for (var k = 0; k < this.inputs.length; k++) {
            if (!this.inputs[k].enabled) {
                this.inputs[k].enable();
            }
        }
        if (this.paintInterval == null) {
            this.paintInterval = setInterval((function (self) { return function () { self.paint(); } })(this), this.frameRate);
        }
    },
    resize: function resize() {
        if (this.isActive) {
            var pc = this.paintingContext;
            pc.frontLayer.canvas.height = $(window).height();
            pc.frontLayer.canvas.width = $(window).width();
            pc.backLayer.canvas.height = $(window).height();
            pc.backLayer.canvas.width = $(window).width();
            this.paint();
        }
    },
    paint: function paint() {
        var pc = this.paintingContext,
            fc = this.paintingContext.frontLayer,
            bc = this.paintingContext.backLayer;

        for (var k = 0; k < this.inputs.length; k++) {
            if (this.inputs[k].enabled) {
                this.inputs[k].update(pc);
            }
        }
        fc.clearRect(0, 0, fc.canvas.width, fc.canvas.height);
        pc.viewPort.setPixelSize(fc.canvas.width, fc.canvas.height);
        pc.gameObject = null;
        
        for (var i = 0; i < this.painters.length; i++) {
            this.painters[i].paint(pc);
        }

        for (var i = 0; i < this.objects.length; i++) this.objects[i].updateLocation(pc);

        // Order painters by distance
        util.orderByDistance(this.objects);

        // Paint away!
        for (var i = 0; i < this.objects.length; i++) {
            var o = this.objects[i];
            o.tick();
            if (o.locationRelativeToCamera != null && o.locationRelativeToCamera.visible) {
                pc.gameObject = o;
                o.getPainter().paint(pc);
            }
        }
    }
});
